package com.harmontronics.app.pedestal;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

import androidx.camera.camera2.Camera2Config;
import androidx.camera.core.CameraXConfig;
import androidx.multidex.MultiDex;

import com.baidu.location.LocationClient;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ProcessUtils;
import com.blankj.utilcode.util.Utils;
import com.harmontronics.app.pedestal.db.CustomSQLiteOpenHelper;
import com.harmontronics.app.pedestal.db.LocalDataSQLiteOpenHelper;
import com.harmontronics.app.pedestal.db.model.DaoMaster;
import com.harmontronics.app.pedestal.picture.PictureSelectorEngineImp;
import com.harmontronics.app.pedestal.update.OkGoHttpService;
import com.luck.picture.lib.app.IApp;
import com.luck.picture.lib.app.PictureAppMaster;
import com.luck.picture.lib.engine.PictureSelectorEngine;
import com.lzy.okgo.OkGo;
import com.tencent.bugly.crashreport.CrashReport;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.entity.UpdateError;
import com.xuexiang.xupdate.listener.OnUpdateFailureListener;

import org.jetbrains.annotations.NotNull;

import java.io.FileFilter;

public class App extends Application implements IApp, CameraXConfig.Provider {
    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);

        CrashReport.initCrashReport(getApplicationContext(), "3b0b8b00c8", false);

        LogUtils.getConfig().setGlobalTag("HanCode")
                .setFilePrefix("Log")
                .setFileFilter(LogUtils.E)
                .setSaveDays(1)
                .setLog2FileSwitch(true)
                .setLogSwitch(true);

        OkGo.getInstance().init(this);

        XUpdate.get()
                .debug(false)
                .isWifiOnly(false)                                               // By default, only version updates are checked under WiFi
                .isGet(true)                                                    // The default setting uses Get request to check versions
                .isAutoMode(false)// The default setting is non automatic mode
                .setOnUpdateFailureListener(new OnUpdateFailureListener() {     // Set listening for version update errors
                    @Override
                    public void onFailure(UpdateError error) {
                    }
                })
                .supportSilentInstall(true)
                .setIUpdateHttpService(new OkGoHttpService())
                .init(this);

        PictureAppMaster.getInstance().setApp(this);

        LocalDataSQLiteOpenHelper openHelper = new LocalDataSQLiteOpenHelper();
        DaoMaster daoMaster = new DaoMaster(openHelper.getReadableDb());

        LocationClient.setAgreePrivacy(true);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // 安卓9.0后不允许多进程使用同一个数据目录
            String processName = ProcessUtils.getCurrentProcessName();
            if (!getPackageName().equals(processName)) {
                try {
                    WebView.setDataDirectorySuffix(processName);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }

    @Override
    public Context getAppContext() {
        return this;
    }

    @Override
    public PictureSelectorEngine getPictureSelectorEngine() {
        return new PictureSelectorEngineImp();
    }

    @NotNull
    @Override
    public CameraXConfig getCameraXConfig() {
        return Camera2Config.defaultConfig();
    }
}
